package com.wx.forum.controller;

import com.wx.forum.common.AppResult;
import com.wx.forum.common.ResultCode;
import com.wx.forum.model.Board;
import com.wx.forum.service.impl.BoardServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("board")
@Api(tags = "板块接口")
public class BoardController {
    @Resource
    private BoardServiceImpl boardService;

    @Value("${forum.index.board-num:9}")//如果没有读取到配置信息，默认是9
    private Integer boardNum;
    /**
     * 获得板块信息
     * @return
     */
    @RequestMapping("getTopBoard")
    @ApiOperation("获得所有板块信息")
    public AppResult<List<Board>> getTopBoard(){
        //1.获取信息
        List<Board> boardList = boardService.selectByNum(boardNum);
        if(boardList == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            boardList = new ArrayList<>();
        }
        return AppResult.success(boardList);

    }
    @GetMapping("getById")
    @ApiOperation("根据id获取板块信息")
    public AppResult<Board> getById(@ApiParam("板块id") @RequestParam("id") @NonNull Long id){
        //1.获取板块信息
        Board board = boardService.selectById(id);
        //2.验证信息是否有效
        if(board == null || board.getDeleteState() == 1){
            //打印信息
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //返回信息
            return AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        //3.返回数据
        return AppResult.success(board);
    }

}
